package org.finesys.common.security.authentication.sms;

import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.finesys.common.constants.SecurityConstants;
import org.finesys.common.security.authentication.base.OAuth2ResourceOwnerBaseAuthenticationConverter;
import org.finesys.common.security.core.util.OAuth2EndpointUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.util.MultiValueMap;

/**
 *
 */
public class OAuth2ResourceOwnerSmsAuthenticationConverter extends OAuth2ResourceOwnerBaseAuthenticationConverter<OAuth2ResourceOwnerSmsAuthenticationToken> {

    public static final AuthorizationGrantType SMS = new AuthorizationGrantType(SecurityConstants.SMS);

    /**
     * 是否支持
     */
    public boolean support(String grantType) {
        return SMS.getValue().equals(grantType);
    }

    @Override
    public void checkParams(HttpServletRequest request) {
        MultiValueMap<String, String> parameters = OAuth2EndpointUtils.getParameters(request);
        //手机号和验证码
        OAuth2EndpointUtils.checkMustParameters(parameters, SecurityConstants.MOBILE, SecurityConstants.CAPTCHA);
    }

    @Override
    public OAuth2ResourceOwnerSmsAuthenticationToken bulidToken(Authentication authentication, Set<String> requestedScopes, Map<String, Object> additionalParameters) {
        return new OAuth2ResourceOwnerSmsAuthenticationToken(SMS, authentication, requestedScopes, additionalParameters);
    }
}
